package com.scalar.chiptrack.adaptor.sitime;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.Adaptor;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.CSVParserImpl;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 30 Apr, 2010
 * Time: 5:40:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class CarsemTransactionParser extends JobHandler implements AdaptorConstants
{
    private static Logger m_parserLogger= null;
    private String m_dataOutputDir      = null;

    private HashMap<String, ShippedLot> m_shipDataMap       = new HashMap<String, ShippedLot>();
    private HashMap<String, ShippedLot> m_shipQtyOutDataMap = new HashMap<String, ShippedLot>();
    private HashMap<String, AssemblyLot> m_receiveQtyInMap  = new HashMap<String, AssemblyLot>();
    private HashMap<String, PackingSlipForm> packingSilpMap = new HashMap<String, PackingSlipForm>();
    private ArrayList<Adaptor> transactionList = new ArrayList<Adaptor>();

    public CarsemTransactionParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_CARCEM, LOCATION_TYPE_TEST );
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_CARCEM );
            AdaptorDAO.setLogger( m_parserLogger );
            ShippedLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading/creating Carsem Trnas Parser output directory from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new CarsemTransactionParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    //Execute Method
   public void execute() throws Exception
   {
       AdaptorUtils.printHeader(VENDOR_NAME_CARCEM + " FTP", CONSTANT_PARSER );

       File doneDestDir = new File( m_dataOutputDir + "/done" );

       File wipDataoutDir = new File( m_dataOutputDir );

       File[] fileList = wipDataoutDir.listFiles();

       if ( ( fileList == null ) || ( fileList.length <= 3 ) )
       {
           log( "No file found in directory: " + wipDataoutDir );
           throw new ScalarException( "No file found in directory: " + wipDataoutDir + "----\n\n" );
       }

       ArrayList<File> successFileList   = new ArrayList<File>();
       boolean success             = false;

       try
       {
           for ( int i = 0; i < fileList.length; i++ )
           {
               if ( fileList[i].isDirectory())
               {
                   continue;
               }

               /*if ( ( fileList[i].getName() != null ) && fileList[i].getName().trim().startsWith( CarsemFTPAdaptor.FILE_NAME_TRANSACTION ) )
               {
                   parseDailyTransactionReport ( fileList[i].getAbsolutePath() );

                   successFileList.add( fileList[i] );
               }*/
               if ( ( fileList[i].getName() != null ) && fileList[i].getName().trim().startsWith( CarsemFTPAdaptor.FILE_NAME_EDI_TRANSACTION ) )
               {
                   parseEDIDailyTransactionReport ( fileList[i].getAbsolutePath(),  fileList[i].getName() );

                   successFileList.add( fileList[i] );
               }
               if ( ( fileList[i].getName() != null ) && fileList[i].getName().trim().startsWith( CarsemFTPAdaptor.FILE_NAME_SHIP ) )
               {
                   parseShipReport ( fileList[i].getAbsolutePath() );

                   successFileList.add( fileList[i] );
               }
           }
           success = true;
       }
       catch ( Exception e )
       {
           success = false;
           log( "ERROR!! ERROR!! ERROR!! While parsing Carsem Trans Yield Report and storing File " + e );
           e.printStackTrace();
           throw e;
       }
       if ( !transactionList.isEmpty() )
       {
          AdaptorDAO.updateEDITransactionsData( null, transactionList );
       }
       try
       {
           if ( !m_receiveQtyInMap.isEmpty() )
           {
               log(" Saving the In Transaction report information");

               AdaptorDAO.saveInTransactionData( null, m_receiveQtyInMap.values().iterator() );
           }
           success = true;
       }
       catch (Exception e)
       {
           success = false;
            e.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Yield Lot information into database: " + e );
            throw e;
       }
       try
        {
            // Save Out Transactions
            if ( m_shipQtyOutDataMap.isEmpty() == false )
            {
                log( " Updating Out Transaction Lot Information" );
                AdaptorDAO.saveOutTransactionData( null, m_shipQtyOutDataMap.values().iterator() );
            }
            success = true;
            m_shipQtyOutDataMap.clear();
            m_shipQtyOutDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Out Transaction Lot information into database: " + se );
            throw se;
        }
        if ( !packingSilpMap.isEmpty() )
        {
            AdaptorDAO.updatePackingSlipData( null, packingSilpMap.values().iterator() );
        }

        try
        {
            // Save FG Ship Information
            if ( m_shipDataMap.isEmpty() == false )
            {
                log( " Updating Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_shipDataMap.values().iterator() );
            }
            success = true;
            m_shipDataMap.clear();
            m_shipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Shipping Lot information into database: " + se );
            throw se;
        }
	    int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( CarsemFTPAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, CarsemFTPAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, CarsemFTPAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
        if ( success )
        {
            AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
        }
   }//end of execute method

    private void parseEDIDailyTransactionReport( String fileNamePath, String fileName ) throws Exception
    {
        if ( fileNamePath == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        ShippedLot shippedLot   = null;
        AssemblyLot assemblyLot = null;
        String lotNumber        = null;
        String partNumber       = null;
        String fabLotNo = null;
        try
        {
            File detailWipFile = new File( fileNamePath );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileNamePath + " doesnt exist" );
            }

            log( "\nFound " + fileNamePath );
            log( "\nParsing the file for EDI Transaction Report..." );
            System.out.println( "\nFound " + fileNamePath );
            System.out.println( "\nParsing the file for EDI Transaction Report..." );

            String fileNames = AdaptorRunInfo.getPreviousUpdatedFileList(CarsemFTPAdaptor.ADAPTOR_NAME);
            String[] parsedfileName = fileNames != null ? fileNames.split("<br>"): null;
            boolean saveSheetData = true;
            if ( parsedfileName != null )
            {
                for ( int i=0; i< parsedfileName.length; i++)
                {
                     if (fileName.lastIndexOf(".") != -1 )
                     {
                         if ( parsedfileName[i].equalsIgnoreCase(fileName.substring(0, fileName.lastIndexOf(".") ) ) )
                         {
                            saveSheetData = false;
                             break;
                         }
                     }
                     else
                     {
                         if ( parsedfileName[i].equalsIgnoreCase(fileName) )
                         {
                            saveSheetData = false;
                             break;
                         }
                     }
                }
            }
            Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(0);
            ((CSVParserImpl)parser).setDataStartRowIdx(1);

            result = parser.parse();
            if ( result == null || result.getHeadings() == null)
            {
                System.out.println(" ******************** Header Row not Found ********************** ");
                return;
            }
            //System.out.println("result.getHeadings() = " + result.getHeadings());
            if ( result != null )
            {
                while ( result.next() )
                {
                    partNumber = result.getString("CustPartNo");
                    if ( partNumber == null)
                    {
                        partNumber= result.getString("WaferPartNo");
                    }
                    lotNumber  = result.getString("CustLotNo");
                    fabLotNo   = result.getString("WaferLotNo");
                    if ( lotNumber != null && lotNumber.indexOf("@") != -1 )
                    {
                        lotNumber = lotNumber.split("@")[0];
                    }
                    if ( fabLotNo != null && fabLotNo.indexOf("#") != -1 )
                    {
                        fabLotNo = fabLotNo.split("#")[0];
                    }
                    String allWafers = null;
                    if ( partNumber != null && partNumber.trim().length() >0  )
                    {
                         Adaptor adaptor = new Adaptor();

                         adaptor.setTransactionCode(result.getString("TranCode"));
                         adaptor.setCustomerPart(partNumber);
                         adaptor.setWaferLot( fabLotNo );
                         adaptor.setWaferNo( result.getInt("WaferNo"));
                         adaptor.setDieQty( result.getInt("DieQty"));
                         adaptor.setTransactionTime(result.getString("TranTime"));
                         adaptor.setLotStatus(result.getString("LotStatus"));
                         adaptor.setWaferMapName(result.getString("WaferMapName"));
                         adaptor.setSystemTime(result.getString("SysEntryTime"));
                         adaptor.setShipFromPlant(null);
                         adaptor.setShipLocation(result.getString("ShipLoc"));
                         adaptor.setAssemblyLot(lotNumber);
                         adaptor.setLotDieQty( result.getInt("LotQty") );
                         adaptor.setBachingLot( result.getString("BatchLotNo") );
                         adaptor.setDateCode(result.getString("DateCode") );
                         adaptor.setVendorName( VENDOR_NAME_CARCEM );
                         if ( saveSheetData )
                         {
                            transactionList.add( adaptor );
                         }

                        String transDate = StringUtils.formatDate(adaptor.getTransactionTime(), "yyyyMMdd HHmmss", "MM/dd/yyyy");
                        
                        //OUT TRANSACTIONS
                        if ( adaptor.getTransactionCode() != null &&
                         ( "Assembly_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) || "Pretest_Out".equalsIgnoreCase( adaptor.getTransactionCode() )
                            || "FT_Out".equalsIgnoreCase( adaptor.getTransactionCode() )   || "TR_Out".equalsIgnoreCase( adaptor.getTransactionCode() )
                            || "TRES_IN".equalsIgnoreCase( adaptor.getTransactionCode() )  || "TRES_Out".equalsIgnoreCase( adaptor.getTransactionCode() )
                            /*|| "TFG_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) */
                            || "FG_Out".equalsIgnoreCase( adaptor.getTransactionCode() )   /*|| "Die_Out".equalsIgnoreCase( adaptor.getTransactionCode() )*/ )  )
                        {
                           if ("TR_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                           {
                               if (lotNumber.indexOf("QA") != -1 )
                               {
                                   lotNumber = lotNumber.substring(0, lotNumber.indexOf("QA")-1 );
                               }
                               //System.out.println("TR_Out lotNumber = " + lotNumber);
                           }
                           if ("Pretest_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                           {
                               if (lotNumber.indexOf("_") != -1 )
                               {
                                   lotNumber = lotNumber.substring(0, lotNumber.indexOf("_"));
                               }
                               //System.out.println("Pretest_Out lotNumber = " + lotNumber);
                           }
                           if ("TRES_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                           {
                        	   lotNumber = adaptor.getBachingLot();

                           }
                           if ( "Die_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                           {
                               shippedLot = ( ShippedLot ) m_shipQtyOutDataMap.get ( partNumber + fabLotNo + transDate+adaptor.getDieQty() );

                               if ( shippedLot == null )
                               {
                                    shippedLot = new ShippedLot();
                                    m_shipQtyOutDataMap.put( partNumber + fabLotNo + transDate+adaptor.getDieQty(), shippedLot );
                               }
                           }
                           else
                           {
                               shippedLot = ( ShippedLot ) m_shipQtyOutDataMap.get ( partNumber + lotNumber + adaptor.getTransactionCode()+ transDate+adaptor.getDieQty() );

                               if ( shippedLot == null )
                               {
                                    shippedLot = new ShippedLot();
                                    m_shipQtyOutDataMap.put( partNumber + lotNumber + adaptor.getTransactionCode()+ transDate+adaptor.getDieQty(), shippedLot );
                               }
                           }
                           int quantity = 0;
                           if ( shippedLot.getShipQty() != null )
                           {
                               try
                               {
                                   quantity = Integer.parseInt( shippedLot.getShipQty() );
                               }
                               catch ( NumberFormatException ignore )
                               {
                                   quantity = 0;
                               }
                               quantity += adaptor.getDieQty();

                           }
                           else
                           {
                             quantity = adaptor.getDieQty();
                           }
                           if ( shippedLot.getWaferID() != null )
                            {
                                allWafers = shippedLot.getWaferID() + ";" + adaptor.getWaferNo();
                            }
                            else
                            {
                                allWafers = adaptor.getWaferNo()+"";
                            }

                            if ( quantity > 0 )
                            {
                               if ("Pretest_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(LOCATION_TYPE_PRE_TEST);
                                   shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                                   shippedLot.setNextProcess( LOCATION_TYPE_FT );
                               }
                               else if ("FG_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(LOCATION_TYPE_PRE_TEST);
                                   shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                                   shippedLot.setNextProcess( LOCATION_TYPE_FT );
                               }
                               else if ("TRES_IN".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(LOCATION_TYPE_TAPE_REEL);
                                   shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                                   shippedLot.setNextProcess( LOCATION_TYPE_TAPE_REEL );
                               }
                               else if ("TRES_OUT".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(LOCATION_TYPE_TAPE_REEL);
                                   shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                                   shippedLot.setNextProcess( LOCATION_TYPE_TAPE_REEL 	 );
                                   shippedLot.setBatchingLot( adaptor.getAssemblyLot() );
                               }
                               else if ("Die_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(LOCATION_TYPE_DIE_BANK);
                                   shippedLot.setCurrentLocation(LOCATION_TYPE_DIE_BANK);
                                   shippedLot.setNextProcess( LOCATION_TYPE_ASSEMBLY );

                                   shippedLot.setFabLotNumber( adaptor.getWaferLot() );
                                   shippedLot.setWaferID( allWafers );
                               }
                               else if ( "FT_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(WIP_TEST_TYPE_FT);
                                   shippedLot.setCurrentLocation(WIP_TYPE_TEST);
                                   shippedLot.setNextProcess( LOCATION_TYPE_TAPE_REEL );
                               }
                               else if ("ASSEMBLY_OUT".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(WIP_TYPE_ASSEMBLY);
                                   shippedLot.setCurrentLocation(WIP_TYPE_ASSEMBLY);
                                   shippedLot.setNextProcess( LOCATION_TYPE_PRE_TEST );
                               }
                               else if ("TR_Out".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                   shippedLot.setProcessType(LOCATION_TYPE_TAPE_REEL);
                                   shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                                   shippedLot.setNextProcess( LOCATION_TYPE_FG );
                               }
                               shippedLot.setTransactionType( adaptor.getTransactionCode() );
                               shippedLot.setVendorName(VENDOR_NAME_CARCEM);
                               shippedLot.setLotNumber( lotNumber );
                               shippedLot.setPartNumber( partNumber );
                               shippedLot.setShipDate(transDate);
                               shippedLot.setShipQty( quantity + "" );
                               shippedLot.setNextLocation(adaptor.getShipLocation());
                           }
                       }

                       //Received QTY TRANSACTIONS
                       if ( adaptor.getTransactionCode() != null &&
                         ( "BG_In".equalsIgnoreCase( adaptor.getTransactionCode() ) || "Assembly_In".equalsIgnoreCase( adaptor.getTransactionCode() ) || "Pretest_In".equalsIgnoreCase( adaptor.getTransactionCode() )
                            || "FT_IN".equalsIgnoreCase( adaptor.getTransactionCode() ) || "TR_In".equalsIgnoreCase( adaptor.getTransactionCode() )
                            /*|| "TRES_In".equalsIgnoreCase( adaptor.getTransactionCode() ) /*|| "Die_In".equalsIgnoreCase( adaptor.getTransactionCode() )*/
                            || "TFG_In".equalsIgnoreCase( adaptor.getTransactionCode() ) ) )
                       {
							String psType = null;
                           if ("BG_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
								psType = LOCATION_TYPE_BACK_GRIND;
						   }
						   else if ("Pretest_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
								psType = LOCATION_TYPE_PRE_TEST;
						   }
						   else if ("Die_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
								psType =LOCATION_TYPE_DIE_BANK;
						   }
						   else if ("TFG_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
								psType =LOCATION_TYPE_FG;
						   }
						   else if ("TRES_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
								psType =LOCATION_TYPE_TAPE_REEL;
						   }
						   else if ( "FT_IN".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
								psType= WIP_TEST_TYPE_FT;
						   }
						   else if ("Assembly_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
							   psType = WIP_TYPE_ASSEMBLY;
						   }
						   else if ("TR_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
						   {
							   psType = LOCATION_TYPE_TAPE_REEL;
						   }
                           int assyQtyIn = 0;
                           if ("Assembly_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                           {
                               assemblyLot = (AssemblyLot) m_receiveQtyInMap.get( partNumber + lotNumber + transDate + adaptor.getLotDieQty() );

                               if (assemblyLot == null)
                               {
                                   assemblyLot = new AssemblyLot( VENDOR_NAME_CARCEM);
                                   m_receiveQtyInMap.put( partNumber + lotNumber + transDate + adaptor.getLotDieQty() , assemblyLot );

                                   assyQtyIn = adaptor.getLotDieQty();
                               }
                           }
                           else
                           {
                                if ("BG_In".equalsIgnoreCase( adaptor.getTransactionCode() ) || "Die_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                                {
                                   assemblyLot = (AssemblyLot) m_receiveQtyInMap.get( partNumber + fabLotNo + transDate );

                                   if (assemblyLot == null)
                                   {
                                       assemblyLot = new AssemblyLot( VENDOR_NAME_CARCEM);
                                       m_receiveQtyInMap.put( partNumber + fabLotNo + transDate , assemblyLot );
                                   }
                                }
                                else
                                {
                                   assemblyLot = (AssemblyLot) m_receiveQtyInMap.get( partNumber + lotNumber + transDate + psType );

                                   if (assemblyLot == null)
                                   {
                                       assemblyLot = new AssemblyLot( VENDOR_NAME_CARCEM);
                                       m_receiveQtyInMap.put( partNumber + lotNumber+ transDate + psType , assemblyLot );
                                   }
                                }

                                if ( assemblyLot.getQtyIn() != null)
                                {
                                   try
                                   {
                                       assyQtyIn = Integer.parseInt( assemblyLot.getQtyIn() );
                                   }
                                   catch ( NumberFormatException ignore )
                                   {
                                       assyQtyIn = 0;
                                   }
                                   assyQtyIn += adaptor.getDieQty();
                                }
                                else
                                {
                                   assyQtyIn = adaptor.getDieQty();
                                }
                           }

                           if ( assyQtyIn > 0 )
                           {
                               
                               assemblyLot.setProcessType( psType );
                               assemblyLot.setQtyIn( assyQtyIn +"");
                               assemblyLot.setDateIn( transDate );
                               if ("Die_In".equalsIgnoreCase( adaptor.getTransactionCode() ) )
                               {
                                    assemblyLot.setLotNumber( fabLotNo );
									assemblyLot.setFabLotNumber( fabLotNo );
                               }
                               else
                               {
                                   assemblyLot.setLotNumber( lotNumber );
                               }
                               assemblyLot.setDeviceName( partNumber );
                           }
                       }
                    }
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the CARSEM Transaction Report");
            ex.printStackTrace();
            throw ex;
        }
    }
    private void parseDailyTransactionReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        ShippedLot shippedLot   = null;
        AssemblyLot assemblyLot = null;
        String lotNumber        = null;
        String partNumber       = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Transaction Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Transaction Report..." );

            Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(-1);
            ((CSVParserImpl)parser).setDataStartRowIdx(0);

            result = parser.parse();
            //System.out.println("result.getHeadings() = " + result.getHeadings());
            while ( result.next() )
            {
                partNumber = result.getString(5);
                lotNumber   =  result.getString(7);                                   

                String transactionDate         = null;
                transactionDate  = result.getString(10);
                 if ( transactionDate != null )
                 {
                     transactionDate  = StringUtils.formatDate(transactionDate, "yyyyMMdd HHmmss","MM/dd/yyyy HH:mm:ss");
                 }
                if ( partNumber != null && partNumber.trim().length() >0 && lotNumber != null && lotNumber.trim().length() > 0 )
                {                       
                    if ( result.getString(3) != null &&
                     ( "POST_TEST_OUT".equalsIgnoreCase( result.getString(3) ) || "PRETEST_OUT".equalsIgnoreCase( result.getString(3) ) || "TEST_OUT".equalsIgnoreCase( result.getString(3) ) || "FT_OUT".equalsIgnoreCase( result.getString(3) )
                        || "ASSEMBLY_OUT".equalsIgnoreCase( result.getString(3) ) || "TNR_OUT".equalsIgnoreCase( result.getString(3) ) ) || "TR_OUT".equalsIgnoreCase( result.getString(3) ) )
                    {

                       shippedLot = ( ShippedLot ) m_shipQtyOutDataMap.get ( partNumber + lotNumber + transactionDate );

                       if ( shippedLot == null )
                       {
                            shippedLot = new ShippedLot();
                            m_shipQtyOutDataMap.put( partNumber + lotNumber + transactionDate, shippedLot );
                       }

                       int quantity = 0;
                       if ( shippedLot.getShipQty() != null )
                       {
                           try
                           {
                               quantity = Integer.parseInt( shippedLot.getShipQty() );
                           }
                           catch ( NumberFormatException ignore )
                           {
                               quantity = 0;
                           }
                           quantity += result.getInt(9);

                       }
                       else
                       {
                         quantity = result.getInt(9);
                       }

                       if ( quantity > 0 && transactionDate != null )
                       {
                           if ("POST_TEST_OUT".equalsIgnoreCase( result.getString(3) ) || "PRETEST_OUT".equalsIgnoreCase( result.getString(3) ) )
                           {
                               shippedLot.setProcessType(LOCATION_TYPE_PRE_TEST);
                               shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                           }
                           else if ( "TEST_OUT".equalsIgnoreCase( result.getString(3) ) || "FT_OUT".equalsIgnoreCase( result.getString(3) ) )
                           {
                               shippedLot.setProcessType(WIP_TEST_TYPE_FT);
                               shippedLot.setCurrentLocation(WIP_TYPE_TEST);
                           }
                           else if ("ASSEMBLY_OUT".equalsIgnoreCase( result.getString(3) ) )
                           {
                               shippedLot.setProcessType(WIP_TYPE_ASSEMBLY);
                               shippedLot.setCurrentLocation(WIP_TYPE_ASSEMBLY);
                           }
                           else if ("TNR_OUT".equalsIgnoreCase( result.getString(3) ) || "TR_OUT".equalsIgnoreCase( result.getString(3) ) )
                           {
                               shippedLot.setProcessType(LOCATION_TYPE_TAPE_REEL);
                               shippedLot.setCurrentLocation(LOCATION_TYPE_CLASS_STORE);
                           }
                           shippedLot.setVendorName(VENDOR_NAME_CARCEM);
                           shippedLot.setLotNumber( lotNumber );
                           shippedLot.setPartNumber( partNumber );
                           shippedLot.setShipDate( transactionDate );
                           shippedLot.setShipQty( quantity + "" );
                           shippedLot.setFabLotNumber( lotNumber );
                       }
                   }

                   //Received QTY
                   if ( result.getString(3) != null &&
                     ( "POST_TEST_IN".equalsIgnoreCase( result.getString(3) ) || "PRETEST_IN".equalsIgnoreCase( result.getString(3) ) || "FG_IN".equalsIgnoreCase( result.getString(3) )
                        || "TEST_IN".equalsIgnoreCase( result.getString(3) ) || "FT_IN".equalsIgnoreCase( result.getString(3) ) || "ASSEMBLY_IN".equalsIgnoreCase( result.getString(3) )
                        || "TNR_IN".equalsIgnoreCase( result.getString(3) ) ) || "TR_IN".equalsIgnoreCase( result.getString(3) )  )
                   {
                       String psType = null;
                       if ("POST_TEST_IN".equalsIgnoreCase( result.getString(3) ) || "PRETEST_IN".equalsIgnoreCase( result.getString(3) ) )
                       {
                            psType = LOCATION_TYPE_PRE_TEST;
                       }
                       else if ("FG_IN".equalsIgnoreCase( result.getString(3) ) )
                       {
                            int pretestProcessSepId = NewAdaptorDAO.getProcessStepId( partNumber, LOCATION_TYPE_CLASS_STORE, LOCATION_TYPE_PRE_TEST );
                            if ( pretestProcessSepId > 0 )
                            {
                               psType =LOCATION_TYPE_PRE_TEST;
                            }
                            else
                            {
                                psType =LOCATION_TYPE_FG;
                            }

                       }
                       else if ( "TEST_IN".equalsIgnoreCase( result.getString(3) ) || "FT_IN".equalsIgnoreCase( result.getString(3) ) )
                       {
                            psType= WIP_TEST_TYPE_FT;
                       }
                       else if ("ASSEMBLY_IN".equalsIgnoreCase( result.getString(3) ) )
                       {
                           psType = WIP_TYPE_ASSEMBLY;
                       }
                       else if ("TNR_IN".equalsIgnoreCase( result.getString(3) ) || "TR_IN".equalsIgnoreCase( result.getString(3) ) )
                       {
                           psType = LOCATION_TYPE_TAPE_REEL;
                       }
                       assemblyLot = (AssemblyLot) m_receiveQtyInMap.get( partNumber + lotNumber + psType + transactionDate );

                       if (assemblyLot == null)
                       {
                           assemblyLot = new AssemblyLot( VENDOR_NAME_CARCEM);
                           m_receiveQtyInMap.put( partNumber + lotNumber+ psType+ transactionDate , assemblyLot );
                       }

                       int assyQtyIn = 0;
                       if ( assemblyLot.getQtyIn() != null)
                       {
                           try
                           {
                               assyQtyIn = Integer.parseInt( assemblyLot.getQtyIn() );
                           }
                           catch ( NumberFormatException ignore )
                           {
                               assyQtyIn = 0;
                           }

                           assyQtyIn += result.getInt(9);

                       }
                       else
                       {
                           assyQtyIn = result.getInt(9);
                       }

                       if ( assyQtyIn > 0 && transactionDate != null )
                       {
                           assemblyLot.setProcessType( psType );
                           assemblyLot.setQtyIn( assyQtyIn +"");
                           assemblyLot.setDateIn( transactionDate );
                           assemblyLot.setLotNumber( lotNumber );
                           assemblyLot.setDeviceName( partNumber );
                           assemblyLot.setFabLotNumber( lotNumber );
                       }
                   }

                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the Carsem Transaction Report");
            ex.printStackTrace();
            throw ex;

        }
    }

    private void parseShipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        PackingSlipForm packingSlipForm = null;
        String partNumber = null;
        String lotNumber  = null;
        String shipDate   = null;
        String shipTo     = null;
        ShippedLot shippedLot = null;
        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Ship Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Ship Report..." );

            Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(1);
            ((CSVParserImpl)parser).setDataStartRowIdx(2);

            result = parser.parse();
            //System.out.println("result.getHeadings() = " + result.getHeadings());
            while ( result.next() )
            {
                partNumber = result.getString("Target Device");
                lotNumber  = result.getString("Lotno");
                shipDate   = result.getString("Shipdate");
                shipTo     = result.getString("NEXTLOC");

                if ( shipTo != null && shipTo.indexOf(",") != -1 )
                {
                    shipTo = shipTo.substring(0, shipTo.indexOf(",") );
                }
                
                if ( result.getString("Drop Flag") != null && result.getString("Drop Flag").trim().length() >0
                        && "Y".equalsIgnoreCase( result.getString("Drop Flag") ) && result.getString("PACKING SLIP#") != null && result.getString("PACKING SLIP#").trim().length() > 0 )
                {
                    packingSlipForm = new PackingSlipForm();
                    packingSlipForm.setNoOfBoxes( result.getString("NO.OF.BOX"));
                    packingSlipForm.setWeight( result.getString("GROSS WEIGHT"));
                    if ( result.getString("PACKING SLIP#").indexOf("PS-") != -1 )
                    {
                        packingSlipForm.setLabel( result.getString("PACKING SLIP#") );
                    }
                    else
                    {
                        packingSlipForm.setLabel( "PS-" + result.getString("PACKING SLIP#") );
                    }
                    packingSlipForm.setHawb(result.getString("Hawb") );
                    packingSlipForm.setMawb(result.getString("Mawb") );
                    packingSlipForm.setAirWayBillNo( result.getString("Mawb") );
                    packingSlipForm.setEtd(result.getString("Etd") );
                    packingSlipForm.setEta(result.getString("Eta") );
                    if ( !packingSilpMap.containsKey( packingSlipForm.getLabel() ) )
                    {
                        packingSilpMap.put( packingSlipForm.getLabel(), packingSlipForm);
                    }
                }
                //SHIPMENT DATA
                if ( result.getString("Drop Flag") != null && result.getString("Drop Flag").trim().length() >0
                        && "N".equalsIgnoreCase( result.getString("Drop Flag") ) )
                {
                    int pretestProcessSepId = NewAdaptorDAO.getProcessStepId( partNumber, LOCATION_TYPE_CLASS_STORE, LOCATION_TYPE_PRE_TEST );
                    if ( pretestProcessSepId <= 0 )
                    {
                        /*shippedLot = (ShippedLot) m_shipDataMap.get(partNumber + lotNumber + shipDate + shipTo);
                        if (shippedLot == null)
                        {
                            shippedLot = new ShippedLot(VENDOR_NAME_CARCEM, LOCATION_TYPE_CLASS_STORE );
                            m_shipDataMap.put(partNumber + lotNumber + shipDate + shipTo, shippedLot);
                        }
                        shippedLot.setProcessType( LOCATION_TYPE_PRE_TEST );
                    }
                    else
                    {*/
                        shippedLot = (ShippedLot) m_shipDataMap.get(partNumber + lotNumber + shipDate + shipTo);
                        if (shippedLot == null)
                        {
                            shippedLot = new ShippedLot(VENDOR_NAME_CARCEM, LOCATION_TYPE_FG );
                            m_shipDataMap.put(partNumber + lotNumber + shipDate + shipTo, shippedLot);
                        }
                        shippedLot.setProcessType( LOCATION_TYPE_FG );
                        shippedLot.setDorpShipment( false );
                    }

                    int quantity = 0;
                    if ( shippedLot.getShipQty() != null )
                    {
                       try
                       {
                           quantity = Integer.parseInt( shippedLot.getShipQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                       try
                       {
                           quantity += result.getInt("Ship Die Qty");
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                    }
                    else
                    {
                       try
                       {
                           quantity = result.getInt("Ship Die Qty");
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                    }
                    if ( quantity > 0 )
                    {
                        shippedLot.setLotNumber( lotNumber );
                        shippedLot.setPartNumber( partNumber.trim() );
                        shippedLot.setShipDate( shipDate );
                        shippedLot.setShipQty( quantity + "");
                        shippedLot.setMawb( "N/A".equalsIgnoreCase(result.getString("Mawb")) ? null : result.getString("Mawb"));
                        shippedLot.setHawb(  "N/A".equalsIgnoreCase(result.getString("Hawb")) ? null : result.getString("Hawb") );
                        //shippedLot.setFlightNumber( "N/A".equalsIgnoreCase(result.getString("Fltno")) ? null : result.getString("Fltno") );
                        shippedLot.setEtd( result.getString("Etd"));
                        shippedLot.setEta( result.getString("Eta"));
                        shippedLot.setInvoiceNumber( result.getString("INVOCIENO") );
                        shippedLot.setCustomerPO(result.getString("CUSTPO"));
                        shippedLot.setNextLocation( shipTo );
                        shippedLot.setFabLotNumber( lotNumber );

                    }
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the Carsem Ship Report");
            ex.printStackTrace();
            throw ex;

        }
    }

    private static void log( String message )
    {

        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            synchronized ( m_parserLogger )
            {
                m_parserLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}
